package cn.sdhuijin.vms.service;

import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import cn.sdhuijin.vms.dao.ICarDao;
import cn.sdhuijin.vms.dao.IOrganizationDao;
import cn.sdhuijin.vms.model.Car;
import cn.sdhuijin.vms.model.CardType;
import cn.sdhuijin.vms.model.Organization;

@Service
public class CarService implements ICarService {
	final Logger logger = LoggerFactory.getLogger(CarService.class);
	@Inject
	private ICarDao carDao;
	@Inject
	private IOrganizationDao organizationDao;

	@Override
	public void save(Car car) {
		// 修改卡号
		car.getCard().setType(CardType.CAR);
		carDao.save(car);
	}

	@Override
	public Page<Car> getList(Integer page, Integer rows) {
		return carDao.findAll(new PageRequest(page, rows));
	}

	@Override
	public void save(Long organizationId, Car car) {
		Organization org = organizationDao.findOne(organizationId);
		car.setOrganization(org);
		carDao.save(car);
	}

	@Override
	public Car load(Long carId) {
		return carDao.findOne(carId);
	}

	@Override
	public void delete(Long carId) {
		carDao.delete(carId);
	}

	@Override
	public Page<Car> getListByOrgIds(List<Long> orgIds, Integer page,
			Integer rows) {
		return carDao.findByOrganizationIdIn(orgIds,
				new PageRequest(page, rows));
	}

}
